<?php

class Kd_Html_Form_Boolean extends Kd_Html_Form_Element {

    private $_yesOption = NULL;
    private $_noOption = NULL;

    public function  __construct() {
        parent::__construct();
        $this->_yesOption = Kd_Html_Config::getYesOption();
        $this->_noOption = Kd_Html_Config::getNoOption();
        $this->addClass('input', 'select');
    }

    public function getYesOption(){
        return $this->_yesOption;
    }
    public function setYesOption($label, $value = 1){
        $this->_yesOption = array('label'=>$label, 'value'=>$value);
        return $this;
    }

    public function getNoOption(){
        return $this->_noOption;
    }
    public function setNoOption($label, $value = 0){
        $this->_noOption = array('label'=>$label, 'value'=>$value);
        return $this;
    }

    protected function renderReadonly(){
        return '<span class="readonly">'.($this->getValue()? $this->_yesOption['label']: $this->_noOption['label']).'</span>';
    }

    protected function renderEditable(){
        
        if(!$this->getEnabled()){
            $this->addClass('disabled');
        }

        $this->setAttribute('id', $this->getId())
             ->setAttribute('name', $this->getName());
        if(!$this->getEnabled()){
            $this->setAttribute('disabled', 'disabled');
        }

        $result = '<select '.$this->getAttributeHtml().'>';
        $result .= $this->createOptionHtml($this->_yesOption['label'], $this->_yesOption['value']);
        $result .= $this->createOptionHtml($this->_noOption['label'], $this->_noOption['value']);
        return $result.'</select>';
    }

    private function createOptionHtml($label, $value){
        $selected = $this->getValue() == $value? ' selected="selected"': '';
        return sprintf('<option value="%s"%s>%s</option>', $value, $selected, $label);
    }
}